/**
 * 投票
 */
const express = require('express');
const Mock = require('mockjs')
let app = express();

app.use(express.static('.'))
app.use(express.urlencoded({
    extended: false
}))

app.use(express.json())

app.get('/', (req, res) => {
    res.json("json")
})

let data = {
    sno: '07201024',
    name: '梁永豪',
    gender: 1,
    skills: [{
            name: 'html',
            count: 0,
        },
        {
            name: 'css',
            count: 0,
        },
        {
            name: 'php',
            count: 0,
        },
        {
            name: 'appLet',
            count: 0,
        },
        {
            name: "css3",
            count: 0,
        },
        {
            name: "vue.js",
            count: 0
        },
        {
            name: "Ts",
            count: 0,
        }
    ],
    statification: 0,
    opinion: '',
    count: 0
};

app.get('/sampledata', (req, res) => {
    res.json(data);
});

app.post('/sampledata', (req, res) => {
    console.log(req.body)
        // req.body.num = 1000;
        // res.json(req.body)
    data.name = req.body.name;
    data.gender = req.body.gender;
    data.sno = req.body.sno;
    data.opinion += req.body.name + ":" + req.body.opinion + "\n";
    data.skills.forEach(v => {
        req.body.skills.indexOf(v.name) > -1 ? v.count++ : ''
    })

    data.statification = (data.statification * data.count + Number(req.body.statification)) / (data.count + 1);
    data.count++;
    console.log(data.statification)
    console.log((data.statification))
        // data.name = Mock.mock('@cname')//随机生成
        // data.opinion = Mock.mock('@cparagraph')//随机生成

    res.json(data)


});

app.listen(8090)